<?php
/**
 * 描述
 * Created by Paco
 * Date: 2019/1/24 Time: 14:27
 */

namespace App\Http\Controllers\Admin\Product;


use App\Http\Controllers\Controller;
use App\Http\Request\Product\CategoryStore;
use App\Repositories\Product\CategoryRepository;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    private $category;

    public function __construct(CategoryRepository $category)
    {
        $this->category = $category;
    }

    public function index()
    {
        return $this->category->list();
    }

    public function tree()
    {
        return $this->category->tree();
    }

    public function show($cat_id)
    {
        return $this->category->show($cat_id);
    }

    /**
     *
     * store
     * author Paco
     * createTime 2019/1/24 15:08
     * @param CategoryStore $cat
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(CategoryStore $cat, Request $request)
    {
        $params = $cat->validated($request);
        return $this->category->store($params);
    }

    /**
     *
     * update
     * author Paco
     * createTime 2019/1/24 15:09
     * @param CategoryStore $cat
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(CategoryStore $cat, Request $request)
    {
        $params = $cat->validated($request);
        return $this->category->save($params);
    }

    public function delete($cat_id)
    {
        return $this->category->del($cat_id);
    }

    public function treeNameIds(Request $request)
    {
        $cat_id = $request->input('cat_id', 0);
        return $this->category->treeNameIds($cat_id);
    }
}